/*
 * 信号-槽实现
 * 
 * 信号定义
 * 
 * 历史:
 *  2020-02-26 姚彧 创建
 */

#ifndef __SIGNAL_H__
#define __SIGNAL_H__

#include "Slot.hpp"
#include <vector>
#include <algorithm>
#include <iostream>

template<typename Slot=Slot>
class Signal
{
public:
	typedef Slot			slot_type;

public:
	int bind(const slot_type& slot)
	{
		if(std::find(slots_.begin(), slots_.end(), slot) == slots_.end())
			slots_.push_back(slot);
		else
			std::cout << "slot exists: " << slot.id << std::endl;
		return 0;
	}

	void unbind(const slot_type& slot)
	{
		auto it = std::find(slots_.begin(), slots_.end(), slot);
		if(it != slots_.end())
			slots_.erase(it);
	}

	int emit(void*p)
	{
		for(auto& slot: slots_)
		{
			int res = slot(p);
			if(res)
				return res;
		}
		return 0;
	}

private:
	std::vector<slot_type> slots_;
};

#endif